var operationManager_page={};

operationManager_page.action = {
    initBody:function () {
        operationManager_page.interface.initTablePage();
        operationManager_page.action.bindEvent();
    },
    bindEvent:function () {
        $("#addBtn").click(function () {
            // location.hash = '/user/set/';
            operationManager_page.interface.initFormPage();
            return false;
        });
        $("#cancelBtn").click(function () {
            operationManager_page.action.initBody();
            return false;
        });
        $("#saveBtn").click(function () {
            operationManager_page.action.saveBtnClick();
            return false;
        })
    },
    // 在做action动作时校验数据data
    checkData:function (action, data) {
        var result = AppComm.data.newResult();

        if(data.operationKey==""||data.operationKey==null||data.operationKey.trim()==""){
            result.success = false;
            result.error = "操作关键字 不能为空。";
            return result;
        }

        var flag = 0;
        var params={
            id:data.id,
            operationKey:data.operationKey,
        };
        var onSuccess = function (resultData) {
            if (resultData.success) {
                if(resultData.data!=null){
                    flag++;
                }
            }
        };

        var checkUrl = "";
        if (action=='editSubmit') {
            checkUrl = AppComm.url.getBaseURL()+'/operationManager/ajax/getOperationListByKeyOutCurrent';
        } else if (action=='addSubmit'){
            checkUrl = AppComm.url.getBaseURL()+'/operationManager/ajax/getOperationListByKey';
        }

        AppComm.ajax.execute({
            'url':checkUrl,
            'data':params,
            'async':false,
            'success':onSuccess
        });

        if(flag>0){
            result.success = false;
            result.error = "该操作关键字已存在。";
        } else {
            result.success = true;
        }
        return result;
    },
    saveBtnClick:function () {
        var params = {
            id: $("#id").val(),
            operationKey: $("#operationKey").val(),
            operationName: $("#operationName").val(),
            orderId: $("#orderId").val(),
            comment: $("#comment").val()
        };
        var btnAction = "";
        if (operationManager_page.data.action=='edit') {
            btnAction = "editSubmit";
        } else {
            btnAction = "addSubmit";
        }
        var result = operationManager_page.action.checkData(btnAction, params);
        if (!result.success) {
            layer.alert(result.error, {
                icon: 0,
                skin: 'layer-ext-moon'
            })
        } else {
            var onSuccess = function (resultData) {
                if (resultData.success) {
                    operationManager_page.action.initBody();
                    layer.msg("保存成功！", {icon: 1});
                } else {
                    layer.msg("保存失败！", {icon: 2});
                }
            };
            AppComm.ajax.execute({
                'url':AppComm.url.getBaseURL()+'/operationManager/ajax/saveOperation',
                'data':params,
                'success':onSuccess
            });
        }
    },
    deleteOperation:function (id) {
        var params = {
            id:id
        };
        var onSuccess = function (resultData) {
            if (resultData.success) {
                layer.msg("删除成功！", {icon: 1});
                operationManager_page.data.tableIns.reload();
            }
        };
        AppComm.ajax.execute({
            'url':AppComm.url.getBaseURL()+'/operationManager/ajax/deleteOperation',
            'data':params,
            'success':onSuccess
        });
    }
};

operationManager_page.interface = {
    // 加载表格页
    initTablePage:function () {
        var html = template("operationManagerList_template");
        $(".operationManager_box").html(html);
        layui.use(['table','form'], function(){
            var table = layui.table;
            //执行表格渲染
            var tableIns = table.render({
                height:625,
                url:AppComm.url.getBaseURL()+'/operationManager/ajax/getOperationList',
                elem:"#tb_list",
                page: true,
                limit:14,
                loading:true,
                id:'id',
                cols: [[
                    {
                        field:'operationKey',
                        title:'操作关键字',
                        align:'left',
                        width:250
                    },
                    {
                        field:'operationName',
                        title:'操作名称',
                        align:'left',
                        width:150
                    },{
                        field:'orderId',
                        title:'排序号',
                        edit: 'text',
                        align:'left',
                        width:100
                    },{
                        field:'comment',
                        title:'说明',
                        align:'left',
                        width:300
                    }, {
                        title:'操作',
                        fixed:'right',
                        width:150,
                        align:'center',
                        toolbar:'#toolbar'
                    }
                ]]
            });

            operationManager_page.data.tableIns = tableIns;

            table.on('tool(tb_list)', function (obj) {
                var rowData = obj.data;
                var layEvent = obj.event;
                if (layEvent==='editBtn') {
                    operationManager_page.interface.initFormPage(rowData.id);
                    operationManager_page.action.bindEvent();
                }
                if (layEvent==='deleteBtn') {
                    layer.confirm('确定要进行删除吗？',{icon: 3, title:'提示'},function(index){
                        operationManager_page.action.deleteOperation(rowData.id);
                        layer.close(index);
                    }, function(index){
                        layer.close(index);
                    });
                }
            });
            table.on('edit(tb_list)', function(obj){
                var params ={
                    id:obj.data.id,
                    orderId:obj.value,
                };
                var onSuccess = function (resultData) {
                    if (resultData.success) {
                        layer.msg("排序号已修改！", {icon: 1});
                        operationManager_page.action.initBody();
                    } else {
                        lay.msg("修改失败！", {icon: 2});
                    }
                };
                AppComm.ajax.execute({
                    'url': AppComm.url.getBaseURL() + '/operationManager/ajax/saveOperation',
                    'data': params,
                    'success': onSuccess
                });
            });
            // 查询
            var form = layui.form;
            form.render(null, 'searchForm');
            form.on('submit(searchBtn)', function (data) {
                var postData = {
                    operationName: data.field.operationName,
                };
                operationManager_page.data.tableIns.PageIndex=1;
                tableIns.reload({
                    page: {curr:1},
                    where: postData
                });
                return false;
            });
        });

    },
    // 初始化表单页
    initFormPage:function (dataId) {
        if (dataId==undefined || ''==dataId.trim()) {
            // 添加
            operationManager_page.data.action = "add";
            var html = template("operationManagerForm_template");
            $(".operationManager_box").html(html);
            operationManager_page.action.bindEvent();
            // layui.use('form', function(){
            //     var form = layui.form;
            //     form.verify({
            //         operationKey:function(value){
            //             if((value==""||value==null)){
            //                 return "操作关键字不能为空";
            //             }
            //             var flag = 0;
            //             var params={
            //                 operationKey:value,
            //             };
            //             var onSuccess = function (resultData) {
            //                 if (resultData.success) {
            //                     if(resultData.data!=null){
            //                         flag++;
            //                     }
            //                 }
            //             };
            //             AppComm.ajax.execute({
            //                 'url':AppComm.url.getBaseURL()+'/operationManager/ajax/getOperationListByKey',
            //                 'data':params,
            //                 'async':false,
            //                 'success':onSuccess
            //             });
            //             if(flag>0){
            //                 return "操作关键字已存在";
            //             }
            //
            //         },
            //     });

                // form.on('submit(saveBtn)', function(data){
                //     var onSuccess = function (resultData) {
                //         if (resultData.success) {
                //             layer.msg("保存成功！");
                //             roleManager_page.action.initBody();
                //         } else {
                //             layer.msg("保存失败！");
                //         }
                //     };
                //     AppComm.ajax.execute({
                //         'url':AppComm.url.getBaseURL()+'/operationManager/ajax/saveOperation',
                //         'data':data.field,
                //         'success':onSuccess
                //     });
                // });
            // });

        } else {
            // 编辑
            operationManager_page.data.action = "edit";
            layer.load();
            var params = {
                id:dataId
            };
            var onSuccess = function (resultData) {
                layer.closeAll('loading');
                var html = template("operationManagerForm_template", resultData.data);
                $(".operationManager_box").html(html);
                operationManager_page.action.bindEvent();
                // layui.use('form', function(){
                //     var form = layui.form;
                //     form.verify({
                //         operationKey:function(value){
                //             if((value==""||value==null)){
                //                 return "操作关键字不能为空";
                //             }
                //
                //
                //         },
                //     });

                    // form.on('submit(saveBtn)', function(data){
                    //     data.field.id = dataId;
                    //     var onSuccess = function (resultData) {
                    //         if (resultData.success) {
                    //             layer.msg("保存成功！");
                    //             roleManager_page.action.initBody();
                    //         } else {
                    //             layer.msg("保存失败！");
                    //         }
                    //     };
                    //     AppComm.ajax.execute({
                    //         'url':AppComm.url.getBaseURL()+'/operationManager/ajax/saveOperation',
                    //         'data':data.field,
                    //         'success':onSuccess
                    //     });
                    // });
                // });
            };
            AppComm.ajax.execute({
                'url':AppComm.url.getBaseURL()+'/operationManager/ajax/getOperationById',
                'data':params,
                'success':onSuccess
            });
        }
    },
};

operationManager_page.data = {
    'tableIns':undefined,
    'action':""
};


AppComm.initFrame(operationManager_page);